package algorithm;

public class LeetCode50 {

    public static double myPow(double x, int n) {
        if (n == 0) {
            return 1;
        }
        boolean negativeNUmber = false;
        float result = 1;
        if (n < 0) {
            negativeNUmber = true;
            n = (-1) * n;
        }
        for (int i = 0; i < n;i++) {
            result *= x;
        }
        if (negativeNUmber) {
            result = 1 / result;
        }
        return result;
    }

    public static double quickMul(double x, int n) {
        if (n == 0) {
            return 1.0f;
        }
        double y = quickMul(x, n / 2);
        return n % 2 == 0 ? y*y : y*y*x;
    }
    public static double myPow2(double x, int n) {
        return n > 0 ? quickMul(x, n) : 1.0 / quickMul(x, -n);
    }

    public static void main(String[] args) {
        System.out.println(myPow(2.00000, 10) + "");
        System.out.println(myPow(2.10000, 3) + "");
        System.out.println(myPow(2.00000, -2) + "");
    }
}
